package first.codeTop.meituan;


public class Leetcode88 {
    // 合并两个有序数组
    public static void main(String[] args) {
        int[] nums1 = {1,2,3,0,0,0};
        int m = 3;
        int[] nums2 = {2,5,6};
        int n = 3;
        merge(nums1, m, nums2, n);
        for (int i : nums1) {
            System.out.print(i+" ");
        }
    }

    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        int[] nums = new int[m];
        for (int i=0; i<m; i++) {
            nums[i] = nums1[i];
        }
        int i = 0;
        int j = 0;
        int flag = 0;
        while (i < m && j < n) {
            if (nums[i] <= nums2[j]) {
                nums1[flag++] = nums[i++];
            } else {
                nums1[flag++] = nums2[j++];
            }
        }
        while (i < m) {
            nums1[flag++] = nums[i++];
        }
        while (j < n) {
            nums1[flag++] = nums2[j++];
        }
    }
}
